/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

import javax.swing.JWindow;

public class ImageWindow extends JWindow {
	
	private static final int TWELVE = 12;
	private static final Font TEXT_FONT = new Font( null, Font.PLAIN, TWELVE);
	private static final Color TEXT_COLOR = new Color( 210, 210, 210 );
	
	private Image offScreenImage = null;

	public ImageWindow() throws Exception {
		initialize();
	}
	
	private void initialize() throws Exception {
		
		this.setSize( 16, 16 );
		this.setBackground(Color.WHITE);
		Toolkit t = this.getToolkit();
		this.setLocation(
			(int)((t.getScreenSize().getWidth() - this.getWidth())/2),
			(int)((t.getScreenSize().getHeight() - this.getHeight())/2)
		);		
		this.setVisible(true);
	}
	
	@Override
	public final void paint(Graphics g) {
		if (this.offScreenImage==null)
			this.offScreenImage =  this.createImage(this.getWidth(),this.getHeight());
		Graphics2D osG = (Graphics2D)this.offScreenImage.getGraphics();

		render(osG);
		osG.dispose();
		g.drawImage(this.offScreenImage,0,0,this); 
	}

	protected void render(Graphics2D g){
		
		Color gradientTop = new Color( 3,110,146 );
		Color gradientBot = new Color( 0,2, 10 );
		String text = "!";
		
		
		g.setColor( Color.WHITE );
		g.fillRect( 0, 0, getWidth(), getHeight() );
		
		// draw frame
		g.setPaint( 
				new GradientPaint( 
						0, 0, gradientBot ,
						0, 15, gradientTop
				)
		);		
		g.drawLine( 1, 0, 14, 0 );
		g.drawLine( 0, 1, 0, 14 );
		g.drawLine( 15, 1, 15, 14 );
		g.drawLine( 1, 15, 14, 15 );
		
		// draw inner content
        g.setPaint( 
                new GradientPaint( 
                    0, 0, gradientTop ,
                    0, 15, gradientBot
                )
            );
		g.fillRect( 1, 1, 14, 14 );
		
		// render text
		g.setColor( TEXT_COLOR );
		g.setFont( TEXT_FONT );
		
		FontRenderContext frc = g.getFontRenderContext();
		Rectangle2D rectangle = TEXT_FONT.getStringBounds( text, frc );
		int width = (int) rectangle.getWidth() - 1;
		
		int x = 7 - ( width / 2 );
		g.drawString( text, x, 13 );
		
	}


	

	public static void main(String[] args) throws Exception {
		new ImageWindow().setVisible( true );

	}	

}
